<template>
  <li class="tree-node">
    <div class="tree-node-content">
      <div class="tree-node-info">
        <span v-if="category.children && category.children.length > 0" 
              @click="toggleExpand" 
              class="tree-toggle">
          <i :class="expanded ? 'fas fa-caret-down' : 'fas fa-caret-right'"></i>
        </span>
        <span v-else class="tree-toggle-empty"></span>
        
        <span class="category-name">{{ category.name }}</span>
        <span class="category-meta">
          <span class="category-slug">{{ category.slug }}</span>
          <span :class="['status-badge', category.status === 1 ? 'status-active' : 'status-inactive']">
            {{ category.status === 1 ? '启用' : '禁用' }}
          </span>
        </span>
      </div>
      
      <div class="tree-node-actions">
        <button @click="editCategory" class="btn btn-sm btn-warning">
          <i class="fas fa-edit"></i>
        </button>
        <button @click="addChild" class="btn btn-sm btn-success">
          <i class="fas fa-plus"></i>
        </button>
        <button @click="deleteCategory" class="btn btn-sm btn-danger">
          <i class="fas fa-trash"></i>
        </button>
      </div>
    </div>
    
    <ul v-if="expanded && category.children && category.children.length > 0" 
        class="tree-children">
      <CategoryTreeNode 
        v-for="child in category.children" 
        :key="child.id"
        :category="child"
        @edit="$emit('edit', $event)"
        @delete="$emit('delete', $event)" 
        @add-child="$emit('add-child', $event)"
      />
    </ul>
  </li>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import type { CategoryTreeDTO } from '@/types/category'

const props = defineProps<{
  category: CategoryTreeDTO
}>()

const emit = defineEmits<{
  edit: [category: CategoryTreeDTO]
  delete: [category: CategoryTreeDTO]
  'add-child': [category: CategoryTreeDTO]
}>()

const expanded = ref(true)

const toggleExpand = () => {
  expanded.value = !expanded.value
}

const editCategory = () => {
  emit('edit', props.category)
}

const deleteCategory = () => {
  emit('delete', props.category)
}

const addChild = () => {
  emit('add-child', props.category)
}
</script>

<style scoped>
.tree-node {
  margin-bottom: 5px;
}

.tree-node-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  background: #f8f9fa;
  border-radius: 4px;
  border: 1px solid #e9ecef;
}

.tree-node-info {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
}

.tree-toggle {
  cursor: pointer;
  width: 16px;
  text-align: center;
  color: #666;
}

.tree-toggle-empty {
  width: 16px;
}

.category-name {
  font-weight: 500;
  color: #333;
}

.category-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-left: auto;
  margin-right: 10px;
}

.category-slug {
  color: #666;
  font-size: 12px;
}

.status-badge {
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.status-active {
  background: #d4edda;
  color: #155724;
}

.status-inactive {
  background: #f8d7da;
  color: #721c24;
}

.tree-node-actions {
  display: flex;
  gap: 5px;
}

.tree-children {
  list-style: none;
  padding-left: 30px;
  margin-top: 5px;
}

.btn {
  padding: 4px 8px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 11px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  transition: all 0.2s;
}

.btn-sm {
  padding: 4px 8px;
  font-size: 11px;
}

.btn-warning {
  background: #ffc107;
  color: #212529;
}

.btn-success {
  background: #28a745;
  color: white;
}

.btn-danger {
  background: #dc3545;
  color: white;
}
</style> 